/**  \file appdata.cpp
\brief Application data class functions implementation

Price My House for Nokia mobile platforms.

\author © 2011 Enrico Miglino / Tech Consulting (enrico.miglino@ovi.com)
\author "Evimin Fiyati Price My House" is protected by the copyright law
\author Distributed under the LGPL 3.0 license.
\date 2011

\par Licensing notes

    "Price My House" is free software: you can redistribute it
    and/or modify it under the terms of the Lesser GNU General Public License
    as published by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    "Price My House" is distributed in the hope that it
    will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    Lesser GNU General Public License for more details.

    You should have received a copy of the Lesser GNU General Public License
    along with "Price My House".
    If not, see <http://www.gnu.org/licenses/>.

*/

#include "appdata.h"

/**
  \fn AppData::AppData(QObject *) : QObject()
  \brief AppData constructor

  The constructor initialize the file configuration parameters
  Only whe all the parameters are set correctly the file is loaded
  in the string to be processed by the QML application.
  Due the binding association of the parameters in the QML instance
  of the class when one of the properties is chanded the file
  is automatically reloaded.
  Using this strategy is possible to manage more different configuration
  files at the same time with a unique instance of the class in the QML
  application.
  If more than one json file is needed by the QML application at the same time
  it is needed to instantiate more classes.
  \param parent The QObject parent pointer
 */
AppData::AppData(QObject *parent) : QObject(parent)
{
    m_fname.clear();
    m_language.clear();
    m_folder.clear();
    m_data.clear();
    m_datastream.clear();
}

/**
  \fn QString AppData::buildFName()
  \brief Build the AppData file name

  When this method is called if all the implied properties
  are not null then the full path filename for the requested
  configuration json file is returned else returns a null string.
  The full file name is built as shown below:

  m_folder m_fname '_' m_language '.json'

  The first two characters of the full file name are ':/' meaning
  that the file path is relative to the application current folder.

  \note The m_langauge is optional. If this value is empty
  then the '_xx' language string is not added to the file. This
  is because some configuration files doesn't contains values that
  need localization.

  \return The full path and file name
 */
QString AppData::buildFName() {
    QString fullFileName;

    qDebug() << "AppData::buildFName() m_fname " << m_fname << " m_folder " << m_folder;

    if (m_fname.isNull() || m_folder.isNull()) {
        fullFileName.clear();
    }
    else {

        qDebug() << "AppData::buildFName()QApplication::applicationDirPath()  "<< QApplication::applicationDirPath();

        fullFileName.append(QApplication::applicationDirPath());
        fullFileName.append(m_folder);
        fullFileName.append(m_fname);

        // Check if the optional language string is set or is empty
        if (!m_language.isEmpty()) {
            fullFileName.append("_");
            fullFileName.append(m_language);
        }
        fullFileName.append(FILE_EXTENSION);
    }

    qDebug() << "AppData::buildFName() -> return " << fullFileName;

    return fullFileName;
}

/**
  \fn QString AppData::fname()
  \brief Return AppData file name
  \return m_fname The current application data file name
 */
QString AppData::fname() const
{
    return m_fname;
}

/**
  \fn void AppData::setFname(const QString &)
  \brief AppData file name assignement
  \param &n The QString address of the file name.
 */
void AppData::setFname(const QString &n)
{
    m_fname = n;
}

/**
  \fn QString AppData::language()
  \brief Return the current localization language code
  \return m_fname The current application data file name
 */
QString AppData::language() const
{
    return m_language;
}

/**
  \fn void AppData::setLanguage(const QString &)
  \brief AppData localization language code assignement
  \param &n The QString address of the language code
  \todo Set controls to check the language.
 */
void AppData::setLanguage(const QString &n)
{
    m_language = n;
}

/**
  \fn QString AppData::folder()
  \brief Return the current data folder
  \return m_folder The current application data folder
 */
QString AppData::folder() const
{
    return m_folder;
}

/**
  \fn void AppData::setFolder(const QString &)
  \brief AppData data folder
  \param &n The QString address of the folder data
  \todo Set controls to check the the file/folder existence.
 */
void AppData::setFolder(const QString &n)
{
    m_folder = n;
}

/**
  \fn QString AppData::datastream()
  \brief Return the current data stream read from the configuration file
  \return The data stream from the local file
 */
QString AppData::datastream() const
{
    return m_datastream;
}

/**
  \fn AppData::datafile()
  \brief Return the full parameters data file name.
  \return The built data file name, path and language code insertion.
 */
QString AppData::datafile() const
{
    return m_data;
}

/**
  \fn AppData::createConfFile()
  \brief Initializes the configuration creating the full path file name

  This function, defined as Q_INVOKABLE, should be called when the instantiated
  ApPData is loaded in QML to build the full path file name of the configuration file
  The file name is not build automatically but only when the QML program requires it
  so it is possible to manage the needed properties following the QML program logic.
 */
void AppData::createConfFile() {
    m_data = buildFName();
    m_datastream.clear();
}

/**
  \fn void AppData::getJson()
  \brief Initializes the configuration creating the full path file name

  This function, defined as Q_INVOKABLE, should be called when the instantiated
  ApPData is loaded in QML to build the full path file name of the configuration file
  The file name is not build automatically but only when the QML program requires it
  so it is possible to manage the needed properties following the QML program logic.
 */
void AppData::getJson() {

    if (!m_data.isNull()) {
        if (!QFile::exists(m_data))
            m_datastream.clear();
        else {
            QFile file(m_data);
            if (!file.open(QFile::ReadOnly))
                m_datastream.clear();
            else {
                QByteArray data = file.readAll();
                QTextCodec *codec = QTextCodec::codecForLocale();
                QString str = codec->toUnicode(data);
                m_datastream.append(str);
            }
        }
    }
}
